<?php
/**
 * Created by PhpStorm.
 * User: 热心观众
 * Date: 2018/10/7
 * Time: 14:54
 */

namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class Details extends Controller
{
    public function Details($id)
    {

        $problem = Db::table('zz_problem')
            ->where('zz_problem_id',$id)
            ->find();
        $user = Db::table('zz_user')
            ->where('zz_id',$problem['zz_uid'])
            ->find();
        $comment =  Db::table('zz_comment')
            ->where('zz_problem_id',$problem['zz_problem_id'])
            ->select();

        foreach ($comment as $v){
            $u[] = Db::table('zz_user')
                ->where('zz_id',$v['zz_comment_uid'])
                ->find();
        }

        for ($i=0; $i < sizeof($comment); $i++) {
            for ($j=0; $j < sizeof($u); $j++) {
                if (!empty($u[$i])) {
                    if ($u[$j]['zz_id'] == $comment[$i]['zz_comment_uid']) {
                        $comment[$i]['zz_admin_name'] = $u[$j]['zz_name'];
			$comment[$i]['zz_icon'] = $u[$j]['zz_icon'];
                    }
                }
            }
        }

        // 查询当前 问题id 下有多少评论
        $count = sizeof($comment);

        // 判断当前问题是否是当前用户得
        $sess = Session::get('user');
        if ($sess['name'] == $user['zz_name']){
            $user['user_status'] = 1;
        }

        // 查询所有问题
        $pro = Db::table('zz_problem')->select();
        // 通过 问题id 查询 此问题的所有评论
        for ($i=0;$i<sizeof($pro);$i++){
            $proCom = Db::table('zz_comment')->where('zz_problem_id',$pro[$i]['zz_problem_id'])->select();
            $pro[$i]['num'] = sizeof($proCom);
        }
        $res = [];
        if (sizeof($pro) <= 10){
            $res = $pro;
        } else {
            for ($i=0;$i<=sizeof(10);$i++){
                $res[$i] = $pro[$i];
            }
        }
        // 从session 中获取当前用户的信息
        $users = Session::get('user');
        // 查询当前用户的id
        $userId = Db::table('zz_user')->field('zz_id')->where('zz_name',$users['name'])->find();
        // 查询 当前问题 当前用户是否已经收藏
        $collect = Db::table('zz_collect')->where('zz_user_id',$userId['zz_id'])->where('zz_problem_id',$id)->find();

        return view('index/detail',['problem'=>$problem,
            'user'=>$user,
            'comment'=>$comment,
            'count' => $count,
            'problemId' => $id,
            'res' => $res,
            'collect' => $collect,
            'userId' => $userId
        ]);

    }


    // 提交回答
    public function DoAnswer($id)
    {
        $data = array();
        $data['zz_comment_content'] = input('zz_comment_content');

        $user = session::get('user');
        if (empty($user)){
            $msg = "请先登录，再来评论";
            $url = "../log/login";
            return $this->error($msg,$url);
        }

        $uid = Db::table('zz_user')->where('zz_name',$user['name'])->find();
	$jf['zz_integral'] = $uid['zz_integral'] + 1;
	Db::table('zz_user')->where('zz_name',$user['name'])->update($jf);
        $data['zz_comment_uid'] = $uid['zz_id'];
        $data['zz_problem_id'] = $id;
        $data['zz_comment_time'] = date('Y-m-d');


        $res = Db::table('zz_comment')->insert($data);
        if ($res){
		
            $this->success('评论成功');
        }else{
            $this->error('评论失败');
        }
    }


    // 评论采纳
    public function doStatus()
    {
        // 接收 当前评论的 id
        $id = input('cid');
        // 接收 当前问题的 id
        $pid = input('pid');
        // 先判断 当前问题下是否已经有被采纳回答
        $ProblemComment = Db::table('zz_comment')
                          ->where('zz_problem_id',$pid)
                          ->where('zz_comment_status',1)
                          ->select();

        if (empty($ProblemComment)){
            // 更改 问题状态
            $pdata['zz_problem_status'] = 1;
            Db::table('zz_problem')->where('zz_problem_id',$pid)->update($pdata);
            // 查询当前问题下 是否有积分
            $jf = Db::table('zz_problem')->field('zz_problem_jf')->where('zz_problem_id',$pid)->find();
            // 将 悬赏积分 增加到评论用户上
            // 获取 评论用户的id
            $uid = Db::table('zz_comment')->field('zz_comment_uid')->where('zz_comment_id',$id)->find();
            // 获取 评论用户的积分
            $userJf = Db::table('zz_user')->field('zz_integral')->where('zz_id',$uid['zz_comment_uid'])->find();
            $userData['zz_integral']  = $userJf['zz_integral'] + $jf['zz_problem_jf'];
            // 更新 评论用户数据
            Db::table('zz_user')->where('zz_id',$uid['zz_comment_uid'])->update($userData);

            // 通过问题id 查询 提问问题的用户id
            $pUserId = Db::table('zz_problem')->field('zz_uid')->where('zz_problem_id',$pid)->find();
            // 查询 提问用户 积分
            $pUserJf = Db::table('zz_user')->field('zz_integral')->where('zz_id',$pUserId['zz_uid'])->find();
            $pUserData['zz_integral'] = $pUserJf['zz_integral'] - $jf['zz_problem_jf'];
            // 更新 提问用户数据
            Db::table('zz_user')->where('zz_id',$pUserId['zz_uid'])->update($pUserData);

            // 更改 数据库 评论的状态
            $data['zz_comment_status'] = 1;
            Db::table('zz_comment')->where('zz_comment_id',$id)->update($data);

            $res = [];
            $res['status'] = 1;
            $res['pid'] = $pid;
            return json($res);
        } else {
            $res = [];
            $res['status'] = 0;
            return json($res);
        }

    }

    // 评论删除
    public function doPdel()
    {
        // 获取评论id
        $pid = input('id');
        // 判断是否已经删除
        $userP = Db::table('zz_comment')->where('zz_comment_id',$pid)->find();
        if (empty($userP)){
            $res['status'] = 0;
            return json($res);
        }
        // 删除 此评论在后台的数据
        Db::table('zz_comment')->where('zz_comment_id',$pid)->delete();

        $res = [];
        $res['status'] = 1;
        return json($res);
    }

    // 问题删除
    public function pDoPdel()
    {
        // 接收 问题id
        $pid = input('id');
        // 判断是否已经删除
        $userDel = Db::table('zz_problem')
                    ->where('zz_problem_id',$pid)
                    ->find();

        if (empty($userDel)){
            $res['status'] = 0;
            return json($res);
        }

        // 删除 该问题在后台的数据
        Db::table('zz_problem')->where('zz_problem_id',$pid)->delete();
        // 删除 该问题的所有评论
        Db::table('zz_comment')->where('zz_problem_id',$pid)->delete();
        // 删除 所有收藏的数据
        Db::table('zz_collect')->where('zz_problem_id',$pid)->delete();

        // 返回数据
        $res = [];
        $res['status'] = 1;
        return json($res);
    }

    // 问题收藏
    public function doCollect()
    {
        // 接收Session
        $useres = Session::get('user');
        if (empty($useres)){
            $res['status'] = 0;
            return json($res);
        }
        // 接收用户id
        $uid = input('uid');
        // 接收问题id
        $pid = input('pid');
        // 判断改用户是否已经收藏
        $resCollect = Db::table('zz_collect')->where('zz_user_id',$uid)->where('zz_problem_id',$pid)->find();
        if (!empty($resCollect)){
            $res['status'] = 2;
            return json($res);
        }
        // 定义一个数组 接收用户id和问题id
        $data['zz_user_id'] = $uid;
        $data['zz_problem_id'] = $pid;
        // 插入数据
        Db::table('zz_collect')->insert($data);

        $res['status'] = 1;
        return json($res);
    }

    // 取消收藏
    public function doDisCollect()
    {
        // 接收用户id
        $uid = input('uid');
        // 接收问题id
        $pid = input('pid');
        // 判断改用户是否已经取消收藏
        $resCollect = Db::table('zz_collect')->where('zz_user_id',$uid)->where('zz_problem_id',$pid)->find();
        if (empty($resCollect)){
            $res['status'] = 0;
            return json($res);
        }
        // 通过用户id和问题id 删除 收藏表中的数据
        Db::table('zz_collect')->where('zz_user_id',$uid)->where('zz_problem_id',$pid)->delete();

        $res = [];
        $res['status'] = 1;
        return json($res);
    }



}